home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / pascal / gpc1.1p2 / gpc1 / usr / src / gpc-1.1p2-2.6.3 / borland2ep.doc < prev    next >
Encoding:
Text File  |  1995-04-20  |  11.3 KB  |  401 lines

  1.  
  2. From: berend@beard.nest.nl (Berend de Boer)
  3. Subject: Comparison of Borland Turbo Pascal with Extended Pascal
  4.  
  5. Here a part of the Extended Pascal FAQ which discusses the differences
  6. between Extended Pascal and Turbo Pascal. I wrote this part because in
  7. the beginning I had the same difficulties switching over from Turbo
  8. Pascal to Extended Pascal. I would like to hear any
  9. comments/suggestions/improvements. If you have any other questions,
  10. please let me know.
  11.  
  12. --------------------------cut here----------------------------------------
  13. 6. Comparison of Borland Pascal to the Pascal standards
  14.  
  15.    As mentioned earlier, Turbo Pascal does not conform to any of the Pascal
  16.    standards.  If you carefully chose a subset of unextended Pascal, you
  17.    may be able to port code if you're lucky/careful.
  18.  
  19.    To be fair, Turbo Pascal has some wonderful features that make it very
  20.    powerful in the environments in which it runs.  However, those same
  21.    features are of little use on non Windows/DOS platforms and probably are
  22.    not good candidates for standardization.
  23.  
  24.    There are several Turbo Pascal features which are semantically similar
  25.    to features in unextended Pascal or Extended Pascal.  Here is a list of
  26.    mappings between Turbo Pascal features and Extended Pascal features:
  27.  
  28.    - Case constructs
  29.  
  30.       a. Extended Pascal uses otherwise instead of else.
  31.  
  32.            Borland Pascal:
  33.  
  34.              case c of
  35.                'A' : ;
  36.                'B' : ;
  37.              else ...;
  38.              end;
  39.  
  40.            Extended Pascal
  41.  
  42.              case c of
  43.                'A' : ;
  44.                'B' : ;
  45.              otherwise ...;
  46.              end;
  47.  
  48.       b. Missing cases cause Extended Pascal compilers to halt. In the
  49.          case statement above if you had no otherwise clause and char c
  50.          had the value 'C', you got an error (note, this would be
  51.          unnoticed in Borland Pascal).
  52.  
  53.  
  54.    - Procedure and function types and variables
  55.  
  56.       Here is an area of subtle differences.  Turbo Pascal has true
  57.       procedure/function types but doesn't have standard Pascal's
  58.       procedural/functional parameters.
  59.  
  60.         Borland Pascal
  61.  
  62.           type
  63.             CompareFunction = function(Key1, Key2 : string) : integer;
  64.  
  65.           function Sort(Compare : CompareFunction);
  66.           begin
  67.             ...
  68.           end;
  69.  
  70.  
  71.         Extended Pascal
  72.  
  73.            function Sort(Compare : function(Key1, Key2 : string) : integer);
  74.            begin
  75.              ...
  76.            end;
  77.  
  78.       Moving from Turbo Pascal to Extended Pascal might be difficult
  79.       if the Turbo Pascal program saves, compares, trades, etc. procedure
  80.       values.  For example, an array of procedure values isn't possible
  81.       in Extended Pascal.  Moving the other way is a little easier as
  82.       show by the above examples.
  83.  
  84.  
  85.    - Strings
  86.  
  87.        a. Borland Pascal's string type has a special case, namely string
  88.           without a length meaning the same as string[255]. There is no
  89.           default in Extended Pascal so you have to change all string types
  90.           to string(255). Example:
  91.  
  92.              var
  93.                s : string;
  94.  
  95.           becomes:
  96.  
  97.              var
  98.                s : string(255);
  99.  
  100.           Note also that you have to use parentheses instead of brackets.
  101.  
  102.        b. A nice pitfall is the pointer to string as in:
  103.  
  104.             type
  105.               PString = ^String;
  106.  
  107.           In Extended Pascal this is a pointer to a schema type!! Don't
  108.           forget to translate this to:
  109.  
  110.             type
  111.               string255 = string(255);
  112.               PString = ^string255;
  113.  
  114.           If you indeed want to use String as a schema pointer you can
  115.           define things like:
  116.  
  117.             type
  118.               MyStr : ^String;
  119.             begin
  120.               New(MyStr, 1024);
  121.             end;
  122.  
  123.           to allocate 1024 bytes of string space.
  124.  
  125.        c. As you could see above, Extended Pascal has no 255 byte limit
  126.           for strings. It is however save to assume a limit of about
  127.           32000 bytes. At least Prospero's Extended Pascal limits
  128.           strings to 32760 bytes. GNU Pascal seems to allow larger
  129.           strings.  DEC Pascal limits strings to 65535 bytes.
  130.  
  131.  
  132.    - Constant variables
  133.  
  134.        a. Extended Pascal translates Borland's gruesome:
  135.  
  136.             const
  137.               i:integer = 0;
  138.  
  139.           to:
  140.  
  141.             var
  142.               i : integer value 0;
  143.  
  144.           Much nicer ain't it?
  145.  
  146.        b. Even nicer is that you can assign initialization values to
  147.           types. Like:
  148.  
  149.             type
  150.               MyInteger = integer value 0;
  151.  
  152.             var
  153.               i : MyInteger;
  154.  
  155.           All variables of type MyInteger are automatically initialized to
  156.           0 when created.
  157.  
  158.        c. Constant arrays of type string are translated from:
  159.  
  160.             const
  161.               MyStringsCount = 5;
  162.             type
  163.               Ident = string[20];
  164.             const
  165.               MyStrings : array [1..MyStringsCount] of Ident = (
  166.                 'EXPORT', 'IMPLEMENTATION', 'IMPORT', 'INTERFACE',
  167.                 'MODULE');
  168.  
  169.           to:
  170.  
  171.             const
  172.               MyStringsCount = 5;
  173.             type
  174.               Ident = string(20);
  175.             var
  176.               MyStrings : array [1..MyStringsCount] of Ident value [
  177.                 1:'EXPORT'; 2:'IMPLEMENTATION'; 3:'IMPORT';
  178.                 4:'INTERFACE'; 5:'MODULE'];
  179.  
  180.      There seem to be pros and cons to each style.
  181.  
  182.           Some folks don't like having to specify an index since it requires
  183.           renumbering if you want to add a new item to the middle.  However,
  184.      if you index by an enumerated type, you might be able to avoid
  185.      major renumbering by hand.
  186.  
  187.  
  188.    - Variant records
  189.  
  190.        The following construction is not allowed in Extended Pascal:
  191.  
  192.           type
  193.             PersonRec = record
  194.               Age : integer;
  195.               case EyeColor : (Red, Green, Blue, Brown) of
  196.                 Red, Green : (Wears_Glasses : Boolean);
  197.                 Blue, Brown : (Length_of_lashes : integer);
  198.               end;
  199.             end;
  200.  
  201.         The variant field needs an explicit type. Code this as:
  202.  
  203.           type
  204.             EyeColorType = (Red, Green, Blue, Brown);
  205.             PersonRec = record
  206.               Age : integer;
  207.               case EyeColor : EyeColorType of
  208.                 Red, Green : (Wears_Glasses : Boolean);
  209.                 Blue, Brown : (Length_of_lashes : integer);
  210.               end;
  211.             end;
  212.  
  213.  
  214.    - Units
  215.  
  216.        a. You can translate units almost automatically to Extended Pascal
  217.           Modules, taking into account some differences of course.
  218.  
  219.           Extended Pascal does not automatically export everything named
  220.           in a module, but you have to create seperate export clauses.
  221.  
  222.           For example translate the following unit:
  223.  
  224.             unit A;
  225.  
  226.             interface
  227.  
  228.             uses
  229.               B, C;
  230.  
  231.             procedure D;
  232.  
  233.             implementation
  234.  
  235.             procedure D;
  236.             begin
  237.             end;
  238.  
  239.             end.
  240.  
  241.           to this module:
  242.  
  243.             module A interface;
  244.  
  245.             export
  246.               A = (D);
  247.  
  248.             import
  249.               B;
  250.               C;
  251.  
  252.             procedure D;
  253.  
  254.             end.
  255.  
  256.             module A implementation;
  257.  
  258.             procedure D;
  259.             begin
  260.             end;
  261.  
  262.             end.
  263.  
  264.           You can have one or more export clauses and the name of an
  265.           export clause doesn't have to be equal to the name of the
  266.           module.
  267.  
  268.           You also see in this example how to translate the Borland
  269.           Pascal "uses" clause to the Extended Pascal "import" clause.
  270.  
  271.        b. Borland Pascal allows you to have code in a unit that is
  272.           executed once, at startup, to initialize things. You can
  273.           translate this to Extended Pascal's "to begin do ..end"
  274.           structure.
  275.  
  276.           Borland Pascal:
  277.  
  278.              unit A;
  279.  
  280.              interface
  281.  
  282.              implementation
  283.  
  284.              begin
  285.              { do something }
  286.              end.
  287.  
  288.  
  289.           Extended Pascal:
  290.  
  291.              module A interface;
  292.              end.
  293.  
  294.              module A implementation;
  295.  
  296.              to begin do begin
  297.              { do something }
  298.              end;
  299.  
  300.              end.
  301.  
  302.           Extended Pascal also has a "to end do .... end" so you can
  303.           translate Exit handlers also.
  304.  
  305.  
  306.    - Files
  307.  
  308.       Extended Pascal treats files quite differently as Borland Pascal.
  309.       I'm not going to treat file pointers, Get and Put here, but
  310.       instead I focus on special Extended Pascal features.
  311.  
  312.       In Borland Pascal you can read any text file as follows:
  313.  
  314.         var
  315.           t : text;
  316.           Line : string;
  317.         begin
  318.           Assign(t, 'MYTEXT.TXT');
  319.           Reset(t);
  320.           while not eof(t) do  begin
  321.             readln(t, Line);
  322.             writeln(Line);
  323.           end;
  324.         end;
  325.  
  326.       The Assign function associated the textfile T with the file
  327.       MYTEXT.TXT.
  328.  
  329.       In Extended Pascal, files are considered entities external to your
  330.       program. External entities, which don't need to be files, need to
  331.       be bound to a variable your program. Any variable to which
  332.       external entities can be bound needs to be declared bindable. So
  333.       the variable declaration of t becomes:
  334.  
  335.         var
  336.           t : bindable text;
  337.  
  338.       Extended Pascal has the bind function that binds a variable with
  339.       an external entity.  Here is an Extended Pascal procedure that
  340.       emulates the Assign procedure in Turbo Pascal.
  341.  
  342.         procedure Assign(var t : text; protected Name : string);
  343.         var
  344.           b : BindingType;
  345.         begin
  346.           unbind (t);
  347.           b := binding (t);
  348.           b.Name := Name;
  349.           bind (t, b);
  350.           b := binding (t);
  351.         end;
  352.  
  353.       Comments: the unbind procedure unbinds a bindable variable from
  354.       its external entity. If it is not bound, nothing happens. The
  355.       binding function initializes b. We call binding to set some fields
  356.       of the BindingType record. Next we set the name field to the name
  357.       of the file. Calling bind will bind t to the external entity. If
  358.       we now call binding again, we get the current state of t's binding
  359.       type. We can now check for example if the bind has succeeded by:
  360.  
  361.         if not b.bound then
  362.           { do error processing }
  363.  
  364.       Note that Prospero's Pascal defaults to creating the file if it
  365.       does not exists! You need to use Prospero's local addition of
  366.       setting b.existing to true to work-around this.
  367.  
  368.       I've not worked with binary files enough, so no advice yet on how
  369.       to access them, but you access them much the same.
  370.  
  371.       As last an example of getting the size of a file.
  372.  
  373.         function FileSize(filename : String) : LongInt;
  374.         var
  375.           f : bindable file [0..MaxInt] of char;
  376.           b : BindingType;
  377.         begin
  378.           unbind(f);
  379.           b := binding (f);
  380.           b.Name := filename;
  381.           bind(f, b);
  382.           b := binding(f);
  383.           SeekRead(f, 0);
  384.           if empty(f)
  385.            then  file_size := 0
  386.            else  file_size := LastPosition(f) + 1;
  387.           unbind(f);
  388.         end(*file_size*);
  389.  
  390.       Prospero's Extended Pascal has a bug in this case. Replace the
  391.       MaxInt in the type definition of f by a sufficiently large
  392.       integer. GNU Pascal works correct in this case.
  393. --------------------------cut here----------------------------------------
  394.  
  395.  
  396. Groetjes,
  397.  
  398. Berend (-:
  399. CIS: 100120,3121
  400. email: berend@beard.nest.nl
  401.